package com.yazi.mxz.common.db.dao.impl;


import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.StoreMemberDao;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreMember;
import com.yazi.mxz.common.entity.beans.StoreMemberId;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("storeMemberDao")
public class StoreMemberDaoImpl extends BasicDao<StoreMember> implements StoreMemberDao {


	public StoreMemberDaoImpl() {
		super(StoreMember.class);
	}

	@Override
	public Criteria getExample(StoreMember sm, String searchWord) {
		Criteria c = super.getExample(sm, searchWord);
		
		if (null != sm) {
			StoreMemberId smId = sm.getId();
			if (null != smId) {
				if (null != smId.getMemberId()) {
					c.add(Restrictions.eq("member.memberId", smId.getMemberId()));
				} 
				if (null != smId.getStoreId()) {
					c.add(Restrictions.eq("store.storeId", smId.getStoreId()));
				} 
			}
			
			Store store = sm.getStore();
			if (null != store) {
				if (null != store.getStoreId()) {
					c.add(Restrictions.eq("store.storeId", store.getStoreId()));
				} else {
					// ...other Store's fields search
				}
			}
			// ...other StoreMember's fields search
		}
		// ...searchWord search
		return c;
	}

	@Override
	public Page<StoreMember> listByGtUpdateTime(StoreMember sm, Searching si) {
		Long updateTime = sm.getUpdateTime();
		sm.setUpdateTime(null);
		Criteria c = getExample(sm, si.getSearchWord());
		c.add(Restrictions.gt("updateTime", updateTime));
		return list(c, si);
	}
	
}
